//-----------------------------------------------------------------------
// <copyright file="BulkOrderTerritoryDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class BulkOrderTerritoryDAOBase
    {
        #region Common methods
        public virtual BulkOrderTerritory CreateBulkOrderTerritoryFromReader(IDataReader reader)
        {
            BulkOrderTerritory item = new BulkOrderTerritory();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("BulkOrderID"))) item.BulkOrderID = (int)reader["BulkOrderID"];
				if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("TerritoryID"))) item.TerritoryID = (int)reader["TerritoryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("HomeDelivered15Price"))) item.HomeDelivered15Price = (decimal)reader["HomeDelivered15Price"];
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyID"))) item.CurrencyID = (int)reader["CurrencyID"];
					
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBulkOrderTerritoryFromReaderException, ex);
            }
            return item;
        }

		public virtual HDTerritoriesPrices CreateHDTerritoriesPricesFromReader(IDataReader reader)
		{
			HDTerritoriesPrices item = new HDTerritoriesPrices();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("ProductCode"))) item.ProductCode = (string)reader["ProductCode"];
				if (!reader.IsDBNull(reader.GetOrdinal("TerritoryID"))) item.TerritoryID = (int)reader["TerritoryID"];
				if (!reader.IsDBNull(reader.GetOrdinal("HomeDelivered40ContFor15Prices"))) item.HomeDelivered40ContFor15Prices = (decimal)reader["HomeDelivered40ContFor15Prices"];
				if (!reader.IsDBNull(reader.GetOrdinal("HomeDelivered20ContFor15Prices"))) item.HomeDelivered20ContFor15Prices = (decimal)reader["HomeDelivered20ContFor15Prices"];
				if (!reader.IsDBNull(reader.GetOrdinal("Currency"))) item.Currency = (string)reader["Currency"];
				if (!reader.IsDBNull(reader.GetOrdinal("Date"))) item.Date = (DateTime)reader["Date"];
					
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateBulkOrderTerritoryFromReaderException, ex);
			}
			return item;
		}

        #endregion
        
        #region CreateBulkOrderTerritory methods
            
        public virtual void CreateBulkOrderTerritory(BulkOrderTerritory bulkOrderTerritory)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrderTerritoriesInsert");
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderTerritory.BulkOrderID);
				dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, bulkOrderTerritory.CountryID);
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, bulkOrderTerritory.TerritoryID);
               				                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBulkOrderTerritoryException, ex);
            }
        }

        #endregion

        #region UpdateBulkOrderTerritory methods
        
        public virtual void UpdateBulkOrderTerritory(BulkOrderTerritory bulkOrderTerritory)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrderTerritoriesUpdate");            
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderTerritory.BulkOrderID);
				dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, bulkOrderTerritory.CountryID);
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, bulkOrderTerritory.TerritoryID);
              				                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateBulkOrderTerritoryException, ex);
            }
        }
        
        #endregion

        #region DeleteBulkOrderTerritory methods
        public virtual void DeleteBulkOrderTerritory(int bulkOrderID, int territoryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrderTerritoriesDelete");
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderID);
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territoryID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteBulkOrderTerritoryException, ex);
            }
        }

        #endregion

        #region GetBulkOrderTerritory methods
        
        public virtual BulkOrderTerritory GetBulkOrderTerritory(int bulkOrderID, int territoryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrderTerritoriesGet");
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderID);
                dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territoryID);
                
                BulkOrderTerritory bulkOrderTerritory =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        bulkOrderTerritory = CreateBulkOrderTerritoryFromReader(reader);
                        reader.Close();
                    }
                }
                return bulkOrderTerritory;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBulkOrderTerritoryException, ex);
            }
        }
        
        #endregion

		#region GetHDTerritoriesPricesByBulkOrderID methods
        
		public virtual HDTerritoriesPrices GetHDTerritoriesPricesByProductCodeAndTerritoryID(string productCode, int territoryID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("GetHDTerritoriesPricesByProductCodeAndTerritoryID");
                
				dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, productCode);
				dbCommandWrapper.AddInParameter("@TerritoryID", DbType.Int32, territoryID);
				                
				HDTerritoriesPrices hdTerritoriesPrices =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						hdTerritoriesPrices = CreateHDTerritoriesPricesFromReader(reader);
						reader.Close();
					}
				}
				return hdTerritoriesPrices;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetHDTerritoriesPricesByBulkOrderIDException, ex);
			}
		}
        
		#endregion

        #region GetBulkOrderTerritories methods
        public virtual BulkOrderTerritoryCollection GetBulkOrderTerritories(BulkOrderTerritoryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrderTerritoriesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                BulkOrderTerritoryCollection bulkOrderTerritoryCollection = new BulkOrderTerritoryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        BulkOrderTerritory bulkOrderTerritory = CreateBulkOrderTerritoryFromReader(reader);
                        bulkOrderTerritoryCollection.Add(bulkOrderTerritory);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return bulkOrderTerritoryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBulkOrderTerritoriesException, ex);
            }
        }
        
        public virtual BulkOrderTerritoryCollection GetBulkOrderTerritories(BulkOrderTerritoryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetBulkOrderTerritories(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}